define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 按客户来源
            (() => {
                $.ajax({
                    url: "analysis/analysis/index",
                    type: "post",
                    dataType: "json",
                    data: { action: 'source' },
                    success: function (success) {
                        // console.log(success);
                        // return false;
                        // echarts图表的初始化
                        var SourceCharts = echarts.init(document.getElementById('source'))

                        // 配置选项
                        var option = {
                            title: {
                                text: '客户来源数据分析'
                            },
                            tooltip: {},//工具栏
                            legend: {   //数据信息解释
                                data: ['人数'],
                            },
                            xAxis: {  //X轴的数据
                                data: success.x,
                                axisLabel: { interval: 0, rotate: 30 }
                            },
                            yAxis: {},//y轴的数据
                            series: [{
                                name: '人数',
                                type: 'bar',
                                data: success.y
                            }]
                        }

                        // 让配置生效
                        SourceCharts.setOption(option);
                    }
                })
            })();

            // 按客户消费
            (() => {
                $.ajax({
                    url: "analysis/analysis/index",
                    type: "post",
                    dataType: "json",
                    data: { action: 'record' },
                    success: function (success) {
                        // console.log(success);
                        var data = [];
                        for ($i = 0; $i < success.count.length; $i++) {
                            data.push({
                                // num: success.count[$i],
                                value: success.sum[$i],
                                name: success.name[$i],
                                // num:'共花费了' + success.sum[$i]
                            })
                        }
                        // return false
                        // echarts图表的初始化
                        var RecordCharts = echarts.init(document.getElementById('record'))

                        // 配置选项
                        var option = {
                            title: {
                                text: '客户消费数据分析',
                                left: 'center'
                            },
                            tooltip: {
                                trigger: 'item'
                            },
                            legend: {
                                orient: 'vertical',
                                left: 'left'
                            },
                            series: [
                                {
                                    name: '花销',
                                    type: 'pie',
                                    radius: '50%',
                                    label: {
                                        show: true,
                                        formatter(data) {
                                            // correct the percentage
                                            return data.name + ' (消费了' + data.value + ')';
                                        }
                                    },
                                    data: data,
                                    //阴影
                                    emphasis: {
                                        itemStyle: {
                                            shadowBlur: 10,
                                            shadowOffsetX: 0,
                                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                                        }
                                    }
                                }
                            ]
                        }

                        // 让配置生效
                        RecordCharts.setOption(option);
                    }
                })
            })();

            // 按客户回访
            (() => {
                $.ajax({
                    url: "analysis/analysis/index",
                    type: "post",
                    dataType: "json",
                    data: { action: 'visit' },
                    success: function (success) {
                        // console.log(success);
                        // return false
                        var data = [];
                        for ($i = 0; $i < success.count.length; $i++) {
                            data.push({
                                // value: success.count[$i],
                                value: success.count[$i],
                                name: success.name[$i],
                                // num:'共花费了' + success.sum[$i]
                            })
                        }
                        // console.log(success.count);
                        var sum = success.count.reduce(function f(pre, curr) {
                            return pre + curr;
                        });
                        // console.log(sum);
                        // return false;
                        var add = {
                            value: sum,
                            itemStyle: {
                                // stop the chart from rendering this piece
                                color: 'none',
                                decal: {
                                    symbol: 'none'
                                }
                            },
                            label: {
                                show: false
                            }
                        }

                        data.push(add)
                        // console.log(data);
                        // return false
                        // echarts图表的初始化
                        var VisitCharts = echarts.init(document.getElementById('visit'))

                        // 配置选项
                        var option = {
                            title: {
                                text: '客户回访数据分析',
                                left: 'center'
                            },
                            tooltip: {
                                trigger: 'item'
                            },
                            legend: {
                                top: '10%',
                                left: 'center',
                                // doesn't perfectly work with our tricks, disable it
                                selectedMode: true
                            },
                            series: [
                                {
                                    name: 'Access From',
                                    type: 'pie',
                                    radius: ['40%', '70%'],
                                    center: ['50%', '70%'],
                                    // adjust the start angle
                                    startAngle: 180,
                                    label: {
                                        show: true,
                                        formatter(param) {
                                            // correct the percentage
                                            return param.name + ' (' + param.value + '次)';
                                        }
                                    },
                                    data: data,
                                }
                            ]
                        }

                        // 让配置生效
                        VisitCharts.setOption(option);
                    }
                })
            })();

            // 按客户量
            (() => {
                $.ajax({
                    url: "analysis/analysis/index",
                    type: "post",
                    dataType: "json",
                    data: { action: 'deal' },
                    success: function (success) {
                        // console.log(success);
                        var data = Object.values(success)
                        var max = Math.max(...data)
                        // echarts图表的初始化
                        var DealCharts = echarts.init(document.getElementById('deal'))

                        // 配置选项
                        var option = {
                            title: {
                                text: '客户量数据分析',
                                left: 'center',
                            },
                            polar: {
                                radius: [30, '80%']
                            },
                            radiusAxis: {
                                max: max + 1
                            },
                            angleAxis: {
                                type: 'category',
                                data: ['全部客户', '未成交客户', '已成交客户'],
                                startAngle: 75
                            },
                            tooltip: {},
                            series: {
                                type: 'bar',
                                data: data,
                                coordinateSystem: 'polar',
                                label: {
                                    show: true,
                                    position: 'middle',
                                    formatter: '{b}: {c}'
                                }
                            },
                            animation: false
                        }
                        // 让配置生效
                        DealCharts.setOption(option);
                    }
                })
            })();

            //按注册时间 
            (() => {
                $.ajax({
                    url: "analysis/analysis/index",
                    type: "post",
                    dataType: "json",
                    data: { action: 'createtime' },
                    success: function (success) {
                        console.log(success);

                        var sum1 = success.value.reduce(function f(pre, curr) {
                            return pre + curr;
                        });
                        var datas = [];
                        for ($i = 0; $i < success.name.length; $i++) {
                            datas.push({
                                name: success.name[$i] + '(' + success.value[$i] + '个)',
                                max: sum1 / 2
                            })
                        }
                        console.log(datas);
                        // return false
                        // echarts图表的初始化
                        var CreatetimeCharts = echarts.init(document.getElementById('createtime'))

                        // 配置选项
                        var option = {
                            title: {
                                text: '客户注册时间数据分析',
                                left: 'left',
                            },
                            legend: {
                                data: ['客户注册时间相同个数'],
                                left: 'left',
                                top: '10%',
                            },
                            radar: {
                                indicator: datas
                            },
                            series: [
                                {
                                    name: '客户注册时间相同个数',
                                    type: 'radar',
                                    data: [{
                                        value: success.value,
                                        // name: '客户注册时间'
                                    }]

                                },
                            ]
                        }

                        // 让配置生效
                        CreatetimeCharts.setOption(option);
                    }
                })
            })();

            // 按订单时间
            (() => {
                $.ajax({
                    url: "analysis/analysis/index",
                    type: "post",
                    dataType: "json",
                    data: { action: 'ordertime' },
                    success: function (success) {
                        console.log(success);
                        var sum2 = success.Odate.reduce(function f(pre, curr) {
                            return pre + curr;
                        });
                        var Odatas = [];
                        for ($i = 0; $i < success.Oname.length; $i++) {
                            Odatas.push({
                                name: success.Oname[$i] ,
                                value: success.Odate[$i]
                            })
                        }
                        console.log(Odatas);
                        // return false
                        // echarts图表的初始化
                        var OrdertimeCharts = echarts.init(document.getElementById('ordertime'))

                        // 配置选项
                        var option = {
                            title: {
                                text: '客户订单时间数据分析',
                                left: 'center',
                            },
                            tooltip: {
                                trigger: 'item'
                            },
                            legend: {
                                orient: 'vertical',
                                left: 'left',
                                // doesn't perfectly work with our tricks, disable it
                                selectedMode: true
                            },
                            series: [
                                {
                                    name: '客户订单时间相同个数',
                                    type: 'pie',
                                    radius: '55%',
                                    roseType: 'angle',
                                    label: {
                                        show: true,
                                        formatter(param) {
                                            // correct the percentage
                                            return param.name + ' (' + param.value + '个)';
                                        }
                                    },
                                    data: Odatas,
                                }
                            ]
                        }

                        // 让配置生效
                        OrdertimeCharts.setOption(option);
                    }
                })
            })();
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});